/*! 
  ZcAdmin.js v1.0.1
 */

if (typeof jQuery === "undefined") {
    throw new Error("ZcAdmin requires jQuery");
}

$.ZcAdmin = {};

$.ZcAdmin.options = {
    sidebarToggleSelector: "[data-toggle='offcanvas']",
    sidebarPushMenu: true,
    sidebarExpandOnHover: false,
    enableBoxRefresh: true,
    enableBSToppltip: true,
    BSTooltipSelector: "[data-toggle='tooltip']",
    enableFastclick: false,
    enableControlSidebar: true,
    controlSidebarOptions: {
        toggleBtnSelector: "[data-toggle='control-sidebar']",
        selector: ".control-sidebar",
        slide: true
    },
    enableBoxWidget: true,
    boxWidgetOptions: {
        boxWidgetIcons: {
            collapse: 'fa-minus',
            open: 'fa-plus',
            remove: 'fa-times'
        },
        boxWidgetSelectors: {
            remove: '[data-widget="remove"]',
            collapse: '[data-widget="collapse"]'
        }
    },
    directChat: {
        enable: true,
        contactToggleSelector: '[data-widget="chat-pane-toggle"]'
    },
    colors: {
        lightBlue: "#3c8dbc",
        red: "#f56954",
        green: "#00a65a",
        aqua: "#00c0ef",
        yellow: "#f39c12",
        blue: "#0073b7",
        navy: "#001F3F",
        teal: "#39CCCC",
        olive: "#3D9970",
        lime: "#01FF70",
        orange: "#FF851B",
        fuchsia: "#F012BE",
        purple: "#8E24AA",
        maroon: "#D81B60",
        black: "#222222",
        gray: "#d2d6de"
    },
    screenSizes: {
        xs: 480,
        sm: 768,
        md: 992,
        lg: 1200
    }
};

$(function () {
    "use strict";

    $("body").removeClass("hold-transition");

    _init();

    $.ZcAdmin.layout.activate();

    $.ZcAdmin.controlSidebar.activate();

    $.ZcAdmin.pushMenu.activate($.ZcAdmin.options.sidebarToggleSelector);

    if ($("body").hasClass('sidebar-collapse')) {
        $(".main-sidebar").tooltip({
            position: {
                my: "left+5 center", at: "right center",
                using: function (position) {
                    $(this).css(position);
                    $("<div>")
                        .addClass("arrow")
                        .appendTo(this);
                }
            }
        });
    } else {
        return 1;
    }

    $('.btn-group[data-toggle="btn-toggle"]').each(function () {
        var group = $(this);
        $(this).find(".btn").on('click', function (e) {
            group.find(".btn.active").removeClass("active");
            $(this).addClass("active");
            e.preventDefault();
        });

    });
});

function _init() {
    'use strict';

    $.ZcAdmin.layout = {
        activate: function () {
            var _this = this;
            $('body, html, .wrapper').css('height', 'auto');
            $(window, ".wrapper").resize(function () {
            });
        },

    };

    $.ZcAdmin.pushMenu = {
        activate: function (toggleBtn) {
            var screenSizes = $.ZcAdmin.options.screenSizes;

            $(document).on('click', toggleBtn, function (e) {
                e.preventDefault();

                if ($(window).width() > (screenSizes.sm - 1)) {
                    if ($("body").hasClass('sidebar-collapse')) {
                        $("body").removeClass('sidebar-collapse').trigger('expanded.pushMenu');
                        $('.click-display').css({ 'left': '230px' });
                        $('.main-sidebar').tooltip({
                            position: {
                                my: "left+5 center", at: "right center",
                                using: function (position) {
                                    $(this).css(position);
                                    $("<div>")
                                        .addClass("arrow")
                                        .appendTo(this);
                                }
                            }
                        });
                        $('.main-sidebar').tooltip("disable");
                        if (window.localStorage) {
                            localStorage.removeItem("Folde");
                        } else {
                            $.cookie('Folde', 'sidebar-collapse');
                        }
                    } else {
                        $("body").addClass('sidebar-collapse').trigger('collapsed.pushMenu');
                        $('.click-display').css({ 'left': '60px' });
                        $('.main-sidebar').tooltip({
                            position: {
                                my: "left+5 center", at: "right center",
                                using: function (position) {
                                    $(this).css(position);
                                    $("<div>")
                                        .addClass("arrow")
                                        .appendTo(this);
                                }
                            }
                        });
                        $('.main-sidebar').tooltip("enable");
                        if (window.localStorage) {
                            localStorage.setItem("Folde", 'sidebar-collapse');
                        } else {
                            $.cookie('Folde', 'sidebar-collapse');
                        }
                    }
                }
                else {
                    if ($("body").hasClass('sidebar-open')) {
                        $("body").removeClass('sidebar-open').removeClass('sidebar-collapse').trigger('collapsed.pushMenu');
                        $('.click-display').css({ 'left': '0px' });
                    } else {
                        $("body").addClass('sidebar-open').trigger('expanded.pushMenu');
                        $('.click-display').css({ 'left': '230px' });
                        if ($(".control-sidebar").hasClass("control-sidebar-open")) {
                            $(".control-sidebar").removeClass("control-sidebar-open");
                        }
                    }
                }
            });

            $(".content-wrapper").click(function () {
                if ($(window).width() <= (screenSizes.sm - 1) && $("body").hasClass("sidebar-open")) {
                    $("body").removeClass('sidebar-open');
                }
            });

        },
    };

    /* 
     * 控制侧边栏
     */
    $.ZcAdmin.controlSidebar = {
        activate: function () {
            var _this = this;
            var sidebar = $($.ZcAdmin.options.controlSidebarOptions.selector);
            var btn = $($.ZcAdmin.options.controlSidebarOptions.toggleBtnSelector);

            btn.on('click', function (e) {
                e.preventDefault();
                if (!sidebar.hasClass('control-sidebar-open')
                    && !$('body').hasClass('control-sidebar-open')) {
                    _this.open(sidebar, $.ZcAdmin.options.controlSidebarOptions.slide);
                } else {
                    _this.close(sidebar, $.ZcAdmin.options.controlSidebarOptions.slide);
                }
            });

            var bg = $(".control-sidebar-bg");
            _this._fix(bg);

            if ($('body').hasClass('fixed')) {
                _this._fixForFixed(sidebar);
            } else {
                if ($('.content-wrapper, .right-side').height() < sidebar.height()) {
                    _this._fixForContent(sidebar);
                }
            }
        },
        open: function (sidebar, slide) {
            if (slide) {
                sidebar.addClass('control-sidebar-open');
            } else {
                $('body').addClass('control-sidebar-open');
            }
        },
        close: function (sidebar, slide) {
            if (slide) {
                sidebar.removeClass('control-sidebar-open');
            } else {
                $('body').removeClass('control-sidebar-open');
            }
        },
        _fix: function (sidebar) {
            var _this = this;
            if ($("body").hasClass('layout-boxed')) {
                sidebar.css('position', 'absolute');
                sidebar.height($(".wrapper").height());
                if (_this.hasBindedResize) {
                    return;
                }
                $(window).resize(function () {
                    _this._fix(sidebar);
                });
                _this.hasBindedResize = true;
            } else {
                sidebar.css({
                    'position': 'fixed',
                    'height': 'auto'
                });
            }
        },
        _fixForFixed: function (sidebar) {
            sidebar.css({
                'position': 'fixed',
                'max-height': '100%',
                'overflow': 'auto',
                'padding-bottom': '50px'
            });
        },
        _fixForContent: function (sidebar) {
            $(".content-wrapper, .right-side").css('min-height', sidebar.height());
        }
    };
}

$(window).ready(function () {
    var ScreenHeight = $(window).height();
    var ScrollSidebarMenu = ScreenHeight - 195;
    $('.content-wrapper').css({ 'min-height': ScreenHeight + 'px' });
    $('.scroll-sidebar-menu').slimScroll({ height: ScrollSidebarMenu + 'px' });
});

/*
 * 箱刷新按钮
 */
(function ($) {

    "use strict";

    //获取背景本地存储
    if (localStorage.getItem("BgColor") == undefined) {
        console.log('数据为空');
    } else {
        var bgcolor = localStorage.getItem("BgColor");
        if (bgcolor == 1) {
            $('.sidebar-mini').removeClass('skin-white-light');
            $('.sidebar-mini').removeClass('skin-black-light');
            $('.sidebar-mini').addClass('skin-blue-light');
        } else if (bgcolor == 2) {
            $('.sidebar-mini').removeClass('skin-blue-light');
            $('.sidebar-mini').removeClass('skin-black-light');
            $('.sidebar-mini').addClass('skin-white-light');
        } else {
            $('.sidebar-mini').removeClass('skin-white-light');
            $('.sidebar-mini').removeClass('skin-blue-light');
            $('.sidebar-mini').addClass('skin-black-light');
        }
    };

    //获取左侧边栏折叠本地存储
    if (localStorage.getItem("Folde") == undefined) {
        console.log('数据为空');
    } else {
        $('body').addClass(localStorage.getItem("Folde"));
    };

    $.fn.boxRefresh = function (options) {

        var settings = $.extend({
            trigger: ".refresh-btn",
            source: "",
            onLoadStart: function (box) {
                return box;
            },
            onLoadDone: function (box) {
                return box;
            }

        }, options);

        var overlay = $('<div class="overlay"><div class="fa fa-refresh fa-spin"></div></div>');

        return this.each(function () {
            if (settings.source === "") {
                if (window.console) {
                    window.console.log("Please specify a source first - boxRefresh()");
                }
                return;
            }
            var box = $(this);
            var rBtn = box.find(settings.trigger).first();

            rBtn.on('click', function (e) {
                e.preventDefault();
                start(box);

                box.find(".box-body").load(settings.source, function () {
                    done(box);
                });
            });
        });

        function start(box) {
            box.append(overlay);

            settings.onLoadStart.call(box);
        }

        function done(box) {
            box.find(overlay).remove();

            settings.onLoadDone.call(box);
        }

    };

})(jQuery);

/*
 * 显示框控件
 */
(function ($) {

    'use strict';

    $.fn.activateBox = function () {
        $.ZcAdmin.boxWidget.activate(this);
    };

    $.fn.toggleBox = function () {
        var button = $($.ZcAdmin.boxWidget.selectors.collapse, this);
        $.ZcAdmin.boxWidget.collapse(button);
    };

    $.fn.removeBox = function () {
        var button = $($.ZcAdmin.boxWidget.selectors.remove, this);
        $.ZcAdmin.boxWidget.remove(button);
    };

})(jQuery);

/*
 * 列表自定义组件
 */
(function ($) {

    'use strict';

    $.fn.todolist = function (options) {
        var settings = $.extend({
            onCheck: function (ele) {
                return ele;
            },
            onUncheck: function (ele) {
                return ele;
            }
        }, options);

        return this.each(function () {

            if (typeof $.fn.iCheck !== 'undefined') {
                $('input', this).on('ifChecked', function () {
                    var ele = $(this).parents("li").first();
                    ele.toggleClass("done");
                    settings.onCheck.call(ele);
                });

                $('input', this).on('ifUnchecked', function () {
                    var ele = $(this).parents("li").first();
                    ele.toggleClass("done");
                    settings.onUncheck.call(ele);
                });
            } else {
                $('input', this).on('change', function () {
                    var ele = $(this).parents("li").first();
                    ele.toggleClass("done");
                    if ($('input', ele).is(":checked")) {
                        settings.onCheck.call(ele);
                    } else {
                        settings.onUncheck.call(ele);
                    }
                });
            }
        });
    };
}(jQuery));